package com.qf.web.controller;/*
*王浩政 
*
2022/8/18
16:51
*小米商城
*/

import com.qf.damain.user;
import com.qf.service.UserService;
import com.qf.service.UserServiceImpl;
import com.qf.utils.RandomUtils;
import com.qf.utils.StringUtil;
import jdk.nashorn.internal.ir.RuntimeNode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet("/userservlet")
public class UserServlet extends BaseServlet {
 //   @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//    //   String method=req.getParameter("method");
//    //   if("register".equals(method)){
//    //       register();
////
//    //   }else if("login".equals(method)){
//    //       login();
////
//    //   }
////
//      String methodName=req.getParameter("method");
//      //反射 类对象
//      try {
//
//          Method method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
//          if(method!=null){
//              method.invoke(this,req,resp);
//          }
//      } catch (Exception e) {
//          e.printStackTrace();
//      }
//  }
//
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        this.doGet(req, resp);
//    }
    public String register(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String repassword = req.getParameter("repassword");
        String email = req.getParameter("email");
        String gender = req.getParameter("gender");

        //校验
        if(StringUtil.isEmpty(username)){
            req.setAttribute("msg","您好！您的用户名不能为空");
            //转发
            return "/register.jsp";
        }
        if(StringUtil.isEmpty(password)){
            req.setAttribute("msg","您好！您的密码不能为空");
            //转发
            return "/register.jsp";
        }
        if(!password.equals(repassword)){
            req.setAttribute("msg","两次密码不能不一样");
            //转发
            return "/register.jsp";
        }
        if(StringUtil.isEmpty(email)){
            req.setAttribute("msg","您好！您的邮箱不能为空");
            //转发
            return "/register.jsp";
        }
        //调用业务逻辑
        //校验用户名是否存在
        UserService userService=new UserServiceImpl();
        boolean b=userService.checkUserName(username);
        if (b){
            req.setAttribute("msg","对不起！该用户名已被注册过啦！");
            //转发
            return "/register.jsp";
        }
        //0 未激活 1会员
        //user user=new user(0,username,password,email,gender,0，1);
        /* public user(Integer id, String username, String password, String email, String gender, Integer flag, Integer role, String code) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.email = email;
        this.gender = gender;
        this.flag = flag;
        this.role = role;
        this.code = code;
    }*/
        try {
            //0 未激活 1 会员
            user user=new user(0,username,password,email,gender,0,1, RandomUtils.createActiveCode());
            userService.regist(user);
            //转发 重定向
            //    return "index.jsp";
            return  "redirect:/registerSuccess.jsp";//重定向
        } catch (Exception e) {
            req.setAttribute("msg","对不起！注册失败啦！"+e.getMessage());
            return "/register.jsp";
        }

     }
    public String login(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        System.out.println("登陆成功");
        return null;
    }
    public String code(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        System.out.println("获取验证码");
        return null;
    }

    public String checkUserName(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        //获取前端数据
        System.out.println("我们仍未知道您的用户名是否正确，我们需要确认一下");
        System.out.println("用户名检查中，请稍等");
        String username = req.getParameter("username");
        System.out.println("username:"+username);
        if(StringUtil.isEmpty(username)){
            resp.getWriter().write("1");//不让注册
            return null;
        }
        //调用业务逻辑
        UserService userService=new UserServiceImpl();
        boolean b=userService.checkUserName(username);
        if(b){//true已存在 不能注册
            resp.getWriter().write("1");
            return null;

        }
        resp.getWriter().write("0");
        System.out.println("感谢您的支持！");
        return null;
    }
}
